package com.smartcommunity.entity.vehicle;

import lombok.Getter;
import lombok.Setter;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

import com.alibaba.fastjson.JSON;
import com.smartcommunity.base.common.CacheMap;
import com.smartcommunity.bean.entity.Village;
import com.smartcommunity.entity.people.RealPeopleHouse;

/**
 * @Author: yangyi
 * @Date: 2020/06/24
 * @Description: ES车辆对象
 * @Modified By:
 */
@Getter
@Setter
public class EsBaseVehEntity {

    /**
     * ID
     */
    private String id;
    
    /**
     * 数据类型  1:实有人口  2:车辆 3:房屋  4:vid
     */
    private Integer dataType;

    /**
     * 车牌号
     */
    private String plateNo;

    /**
     * 实有人口ID
     */
    private Long peopleId;
    /**
     * 姓名
     */
    private String peopleName;

    /**
     * 身份证号
     */
    private String credentialNo;

    /**
     * 证件类型
     */
    private String credentialType;

    /**
     * 证件类型
     */
    private String credentialTypeCN;

    /**
     * 联系电话
     */
    private String phoneNo;

    /**
     * 入库时间
     */
    private Timestamp mrowTime;

    /**
     * 汽车图片
     */
    private String carPic;

    /**
     * 小区编码
     */
    private List<String> villageCode;
    
    /**
     * 人房关系
     */
    private List<RealPeopleHouse> detailAddress = new ArrayList<RealPeopleHouse>();
    
    public String toString() {
        return JSON.toJSONString(this);
    }
    
    public static EsBaseVehEntity parseFrom(Vehicle vehicle) {
    	EsBaseVehEntity esBaseVehEntity = new EsBaseVehEntity();
		esBaseVehEntity.setId(vehicle.getId());
		esBaseVehEntity.setPlateNo(vehicle.getPlateNo());
		esBaseVehEntity.setPeopleId(vehicle.getPeopleId());
		esBaseVehEntity.setPeopleName(vehicle.getPeopleName());
		esBaseVehEntity.setCredentialNo(vehicle.getIdcardNo());
		esBaseVehEntity.setPhoneNo(vehicle.getContactTel());
		esBaseVehEntity.setCarPic(vehicle.getCarPic());
        esBaseVehEntity.setCredentialType(vehicle.getCredentialType());
        esBaseVehEntity.setCredentialTypeCN(vehicle.getCredentialTypeCN());
		return esBaseVehEntity;
    }
    
    public static Vehicle parseTo(EsBaseVehEntity esBaseVehEntity, List<String> villageCodeList) {
    	Vehicle vehicle = new Vehicle();
    	vehicle.setId(esBaseVehEntity.getId());
    	vehicle.setPlateNo(esBaseVehEntity.getPlateNo());
    	vehicle.setPeopleId(esBaseVehEntity.getPeopleId());
    	vehicle.setPeopleName(esBaseVehEntity.getPeopleName());
    	vehicle.setIdcardNo(esBaseVehEntity.getCredentialNo());
    	vehicle.setContactTel(esBaseVehEntity.getPhoneNo());
    	vehicle.setCarPic(esBaseVehEntity.getCarPic());
    	if(CollectionUtils.isNotEmpty(esBaseVehEntity.getDetailAddress())) {
        	for(RealPeopleHouse realPeopleHouse : esBaseVehEntity.getDetailAddress()) {
        		if(CollectionUtils.isEmpty(villageCodeList) || villageCodeList.contains(realPeopleHouse.getVillageCode())) {
        			vehicle.setVillageCode(realPeopleHouse.getVillageCode());
        			vehicle.setBuildingNo(realPeopleHouse.getBuildingNo());
        			vehicle.setUnitNo(realPeopleHouse.getUnitNo());
        			vehicle.setHouseNo(realPeopleHouse.getHouseNo());
                    Village village = CacheMap.VILLAGE_INFO_MAP.get(realPeopleHouse.getVillageCode());
        			vehicle.setVillageName(village!=null?village.getVillageName():"");
                	break;
        		}
        	}
        }
    	return vehicle;
    }

}
